//
//  NavSearchBar.swift
//  Venus
//
//  Created by Smith_Yang on 2018/8/5.
//  Copyright © 2018 zhaowang. All rights reserved.
//

import UIKit

class NavSearchBar: UIView {
    
    lazy var createBtn:UIButton = UIButton(type: .custom)
    lazy var searchTextField:UITextField = UITextField()

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}

// MARK:- 加载UI
extension NavSearchBar {
    fileprivate func setupUI(){
        self.backgroundColor = UIColor(red: 23.0 / 255.0, green: 187.0 / 255.0, blue: 254.0 / 255.0, alpha: 1.0)

        self.addSubview(searchTextField)
        self.addSubview(createBtn)
        
        searchTextField.snp.makeConstraints { (make) in
            searchTextField.snp.makeConstraints { (make) in
                make.left.equalTo(self).offset(15)
                make.bottom.equalTo(self).offset(-10)
                make.right.equalTo((self)).inset(43)
                make.height.equalTo(30)
            }
        }
        searchTextField.backgroundColor = UIColor.white
//        searchTextField.backgroundColor = UIColor.init(hex:"f4f4f5")
        searchTextField.layer.cornerRadius = 3
        searchTextField.clipsToBounds = true
        searchTextField.placeholder = "请输入想要搜索内容"
        searchTextField.font = UIFont.systemFont(ofSize: 12)
        let leftView =  UIImageView()
        leftView.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
        
        
        let imgView = UIImageView()
        leftView.addSubview(imgView)
        imgView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 15, height: 15))
            make.center.equalToSuperview()
            make.left.equalTo(5)
        }
        imgView.image = UIImage(named: "icon_search")
        
        searchTextField.leftViewMode = .always
        searchTextField.leftView = leftView
        
        createBtn.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 43, height: 43))
            make.right.equalTo(self)
            make.centerY.equalTo(searchTextField)
        }
        
        createBtn.setImage(UIImage(named: "nav_create"), for: .normal)
        createBtn.setTitle("创建", for: .normal)
        createBtn.titleLabel?.font = UIFont(name: "PingFang-SC-Medium", size: 9.0)
        
        let titleHeight = createBtn.titleLabel?.font.lineHeight;
        let imageHeight = createBtn.imageView?.frame.size.height;
        let spacing = 10
        let totalHeigh = titleHeight! + imageHeight! + 5;
        
        createBtn.imageEdgeInsets = UIEdgeInsets(top: -(titleHeight! - imageHeight!), left: 0, bottom: 0, right: -20)
        createBtn.titleEdgeInsets = UIEdgeInsets(top: CGFloat(spacing), left: -18, bottom: -(totalHeigh - titleHeight!), right: 0)
        createBtn.contentEdgeInsets = UIEdgeInsets(top: -10, left: -10, bottom: 0, right: -18)
    }
}

